package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.BankAccountDao;
import com.JZ.QM.common.dao.TradeRecordDao;
import com.JZ.QM.common.dao.UserAccountDao;
import com.JZ.QM.common.entity.BankAccountEntity;
import com.JZ.QM.common.entity.TradeRecordEntity;
import com.JZ.QM.common.entity.UserAccountEntity;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.dto.BankAccountDTO;
import com.JZ.QM.dto.RechargeAndWithdrawalDTO;
import com.JZ.QM.service.BankAccountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/6/26 20:27
 */
@Slf4j
@Service()
public class BankAccountServiceImpl extends ServiceImpl<BankAccountDao, BankAccountEntity> implements BankAccountService {
    @Resource
    BankAccountDao bankAccountDao;
    @Resource
    UserAccountDao userAccountDao;
    @Resource
    TradeRecordDao tradeRecordDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<BankAccountEntity> page = this.page(
                new Query<BankAccountEntity>().getPage(params),
                new QueryWrapper<BankAccountEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 获取银行卡的状态信息
     * @param bankAccountCard 用户的银行卡号码
     * @return
     */
    @Override
    public BankAccountEntity getBankAccountCard(String bankAccountCard) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //银行账户基本信息
        BankAccountEntity bankAccountEntity=new BankAccountEntity();

        queryWrapper.eq("bank_account_card",bankAccountCard);
        bankAccountEntity=  bankAccountDao.selectOne(queryWrapper);
        return bankAccountEntity;
    }

    /**
     * 更新银行卡账户记录
     * @param bankAccountDTO 账户信息数据传输对象(账户iD,银行卡密码,预留手机号，银行卡代码，)
     * @return
     */
    @Override
    public boolean updateBankAccount(BankAccountDTO bankAccountDTO) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //更新影响的函数
        int affectRow=0;
        //银行账户信息实体
        BankAccountEntity bankAccountEntity=new BankAccountEntity();

        bankAccountEntity.setAccountId(bankAccountDTO.getAccountId());
        bankAccountEntity.setBankAccountAmoount(new BigDecimal(10000000));
        bankAccountEntity.setBankAccountCard(bankAccountDTO.getBankAccountCard());
        bankAccountEntity.setBankAccountPhone(bankAccountDTO.getBankAccountPhone());
        bankAccountEntity.setAccountBankName(bankAccountDTO.getAccountBankName());
        bankAccountEntity.setBankHotline(bankAccountDTO.getBankHotLine());
        affectRow=bankAccountDao.insert(bankAccountEntity);
        if(affectRow != 0){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据账户id获取银行账户信息列表
     * @param accountId 账户id
     * @return
     */
    @Override
    public ArrayList<HashMap<String,Object>> getBankAccountList(Long accountId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //银行账户列表
        ArrayList<BankAccountEntity> bankAccountEntities=new ArrayList<>();
        //待返回的数据格式
        HashMap<String,Object> bankAccountHashMap=new HashMap<>();
        //返回结果集
        ArrayList<HashMap<String,Object>> resultList=new ArrayList<>();

        //查询对应的银行卡账户信息
        queryWrapper.select("bank_account_amoount","bank_account_phone","bank_account_card","bank_hotline","account_bank_name")
                .eq("account_id",accountId);
        //获取银行账户信息列表
        bankAccountEntities= (ArrayList<BankAccountEntity>) bankAccountDao.selectList(queryWrapper);
        if (!bankAccountEntities.isEmpty()){
            //筛选待返回的信息
            for (Iterator<BankAccountEntity> it = bankAccountEntities.iterator(); it.hasNext(); ) {
                BankAccountEntity iter = it.next();
                bankAccountHashMap=new HashMap<>();
                bankAccountHashMap.put("bankAccountAmoount",iter.getBankAccountAmoount());
                bankAccountHashMap.put("bankAccountPhone",iter.getBankAccountPhone());
                bankAccountHashMap.put("bankAccountCard",iter.getBankAccountCard());
                bankAccountHashMap.put("bankHotline",iter.getBankHotline());
                bankAccountHashMap.put("accountBankName",iter.getAccountBankName());
                resultList.add(bankAccountHashMap);
            }
        }
        return resultList;

    }

    /**
     * 账户充值
     * @param rechargeAndWithdrawalDTO 提现数据传输对象
     * @return
     */
    @Transactional
    @Override
    public boolean recharge(RechargeAndWithdrawalDTO rechargeAndWithdrawalDTO,int flag) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //用户账户影响的行数
        int userAccountAffectRow=0;
        //银行账户影响的行数
        int bankAccountAffectRow=0;
        int TradeRecordAffectRow=0;
        //银行账户信息
        BankAccountEntity bankAccountEntity=new BankAccountEntity();
        //用户账号信息
        UserAccountEntity userAccountEntity=new UserAccountEntity();
        TradeRecordEntity tradeRecordEntity=new TradeRecordEntity();

        //更新银行账户信息
        bankAccountEntity.setBankAccountId(rechargeAndWithdrawalDTO.getBankAccountId());
        bankAccountEntity.setBankAccountAmoount(rechargeAndWithdrawalDTO.getBankAccountAmoount());
        bankAccountAffectRow=bankAccountDao.updateById(bankAccountEntity);
        //更新用户平台账户信息
        userAccountEntity.setAccountAmoount(rechargeAndWithdrawalDTO.getUserAccountAmoount());
        userAccountEntity.setAccountId(rechargeAndWithdrawalDTO.getUserAccountId());
        userAccountAffectRow=userAccountDao.updateById(userAccountEntity);
        //将交易信息加入到交易记录中
        tradeRecordEntity.setUserId(rechargeAndWithdrawalDTO.getUserId());
        tradeRecordEntity.setTradeAmoount(rechargeAndWithdrawalDTO.getTradeMoney());
        tradeRecordEntity.setLogicId("QMT"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        tradeRecordEntity.setRelevanceAccountId(rechargeAndWithdrawalDTO.getBankAccountId());
        tradeRecordEntity.setTradeType(flag);
        tradeRecordEntity.setRelevanceAccountType(1);
        tradeRecordEntity.setTradeCategory(flag+5);
        TradeRecordAffectRow=tradeRecordDao.insert(tradeRecordEntity);
        if (userAccountAffectRow !=0 && bankAccountAffectRow !=0 && TradeRecordAffectRow!=0){
            return true;
        } else {
            return false;
        }


    }
}
